/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.common.service;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import etcore.webapp.grouptalk.common.dao.AttachmentDAO;
import etcore.webapp.grouptalk.common.dao.BaseDAO;
import etcore.webapp.grouptalk.common.dao.HibernateSessionFactory;
import etcore.webapp.grouptalk.common.object.Attachment;

/**
 * AttachmentService implements queries and transactions on Attachment
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class AttachmentService extends BaseService {

	@SuppressWarnings("rawtypes")
	@Override
	public void deleteList(List  persistentInstanceList) {

		Session session = HibernateSessionFactory.getSession();
		Transaction tx = session.beginTransaction();
		try {
			BaseDAO dao = new AttachmentDAO();
			dao.deleteList(persistentInstanceList);
			tx.commit();

		} catch (RuntimeException re) {

			tx.rollback();
		} finally {
			if (session != null)
				session.close();
		}

	}

	@Override
	public void deleteSingle(Object persistentInstance) {

		Session session = HibernateSessionFactory.getSession();
		Transaction tx = session.beginTransaction();
		try {
			BaseDAO dao = new AttachmentDAO();
			dao.deleteSingle(persistentInstance);
			tx.commit();

		} catch (RuntimeException re) {

			tx.rollback();
		} finally {
			if (session != null)
				session.close();
		}
	}

	/**
	 * Find Attachment instances
	 * 
	 * @param begin
	 *            start position of finding instances
	 * @param length
	 *            count of instances to be obtained
	 * @return Attachment instances
	 */
	public List<Attachment> findAll(int begin, int length) {
		Session session = HibernateSessionFactory.getSession();

		try {
			AttachmentDAO dao = new AttachmentDAO();
			List<Attachment> result = dao.findAll(begin, length);
			return result;

		} catch (RuntimeException re) {

			re.printStackTrace();
		} finally {
			if (session != null)
				session.close();
		}

		return null;

	}

	/**
	 * Find Attachment instance by id
	 * 
	 * @param id
	 *            Attachment ID
	 * @return Attachment instance
	 */
	public Attachment findById(java.lang.Integer id) {
		Session session = HibernateSessionFactory.getSession();
		try {
			AttachmentDAO dao = new AttachmentDAO();
			Attachment result = dao.findById(id);
			return result;

		} catch (RuntimeException re) {

			re.printStackTrace();
		} finally {
			if (session != null)
				session.close();
		}
		return null;

	}

	/**
	 * Find all instances of Attachment by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @return Attachment instances
	 */
	public List<Attachment> findByProperty(String propertyName, Object value) {
		Session session = HibernateSessionFactory.getSession();

		try {
			AttachmentDAO dao = new AttachmentDAO();
			List<Attachment> result = dao.findByProperty(propertyName, value);
			return result;

		} catch (RuntimeException re) {

			re.printStackTrace();
		} finally {
			if (session != null)
				session.close();
		}

		return null;

	}

	/**
	 * Find Attachment instances by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @param begin
	 *            start position
	 * @param length
	 *            count of instances
	 * @return Attachment instances
	 */
	public List<Attachment> findByProperty(String propertyName, Object value,
			int begin, int length) {
		Session session = HibernateSessionFactory.getSession();

		try {
			AttachmentDAO dao = new AttachmentDAO();
			List<Attachment> result = dao.findByProperty(propertyName, value,
					begin, length);
			return result;

		} catch (RuntimeException re) {

			re.printStackTrace();
		} finally {
			if (session != null)
				session.close();
		}

		return null;
	}

	/**
	 * Find the count of Attachment instances by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @return the count of Attachment instances found
	 */
	public long findSizeByProperty(String propertyName, Object value) {
		Session session = HibernateSessionFactory.getSession();

		try {
			AttachmentDAO dao = new AttachmentDAO();
			long result = dao.findSizeByProperty(propertyName, value);
			return result;

		} catch (RuntimeException re) {

			re.printStackTrace();
		} finally {
			if (session != null)
				session.close();
		}

		return 0;

	}

}